package com.ecity.table {
	import com.ecity.util.HashMap;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	import gs.events.TweenEvent;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class HitButton extends TableDisplayObject{
		private static const CLASS_KEY:String = "button";
		private var disable:Boolean = false;
		private var hitIndex:int=0;
		
		private var hitmap:HashMap = new HashMap();
		
		public function HitButton() {
			
		}
		
		public function hitTest(rx:int, ry:int, key:String):Boolean {
			var layer:Sprite= this.getLayer(key);
			if (layer.getChildAt(0) == null) {
				return false;
			}
			if (!disable) {
				var bmp:Bitmap = layer.getChildAt(0) as Bitmap;
				var rect:Rectangle = new Rectangle(rx * 20 - bmp.x - this.x - layer.x, ry * 20 - this.y - bmp.y - layer.y, 20, 20);
				var isHit:Boolean = bmp.bitmapData.hitTest(new Point(), 0xff, rect);
				if (isHit) {
					hitmap.put("h_"+hitIndex, 0);
					var stid:uint = setTimeout(removeHitPot, 1000, "h_" + hitIndex, stid);
					hitIndex++;
					if (hitmap.size() > 3) {
						TweenLite.to(layer, 0.5, { alpha:0, scaleX:0, scaleY:0, onComplete:onLayerZoomOut, onCompleteParams:[bmp, layer] } );
						disable = true;
						return true;
					}
					return false;
				}
				return false;
			}else {
				return false;
			}
		}
		
		private function removeHitPot():void {
			flash.utils.clearTimeout(arguments[1]);
			hitmap.remove(arguments[0]);
		}
		
		private function onLayerZoomOut(bmp:Bitmap,layer:Sprite):void {

		}
		
		public function addButton(key:String):void {
			var layer:Sprite = this.getLayer(key);
			TweenLite.to(layer, 0.5, { alpha:1, scaleX:1, scaleY:1, onComplete:onLayerZoomIn } );
		}

		private function onLayerZoomIn():void {
			disable = false;
		}
		
	}

}